﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure.StorageClient;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace LCA.Community.DAL.Model
{
    /// <summary>
    /// This class contains a messsage of an user.
    /// 
    /// RowKey = ID of the message
    /// PartitionKey = Sender of the message
    /// </summary>
    public class Message : BasicDataModel
    {
        #region Fields
        public static readonly string HelpPool = "rnwqnrwewqrejrwejwenwqnjwsdjfsdjaejkaesjaejqwerqewrnrnasdfnjasdf";
        private List<string> receiversString;
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Message"/> class.
        /// </summary>
        public Message()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Message"/> class.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="receivers">The receivers.</param>
        public Message(string user, string subject, string body, List<string> recipents)
        {
            RowKey = Guid.NewGuid().ToString();
            PartitionKey = user;
            Subject = subject;
            Body = body;
            Read = false;
            Recipents = recipents;
            Date = DateTime.UtcNow;
        }

        /// <summary>
        /// Determines whether the message is a help request.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if is a help request; otherwise, <c>false</c>.
        /// </returns>
        public bool IsHelpRequest()
        {
            return Subject != null && Subject.StartsWith("Help Request: ");
        }

        /// <summary>
        /// Cleans the subject.
        /// </summary>
        /// <returns>The subject cleaned</returns>
        public String CleanSubject(string subject)
        {
            if (IsHelpRequest())
            {
                return subject.Substring(14);
            }
            else
            {
                return subject;
            }
        }

        /// <summary>
        /// Gets the subject.
        /// </summary>
        /// <returns>The subject.</returns>
        public String GetSubject()
        {
            return CleanSubject(Subject);
        }

        /// <summary>
        /// Gets the subject brief.
        /// </summary>
        /// <returns>The receivers.</returns>
        public String GetSubjectBrief()
        {
            if (Subject.Length <= 57)
            {
                return CleanSubject(Subject);
            }
            else
            {
                return CleanSubject(String.Format("{0}...", Subject.Substring(0, 57)));
            }
        }
        /// <summary>
        /// Equalses the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns><c>true</c> if both objects are equals; otherwise, <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            Message u = obj as Message;
            if ((System.Object)u == null)
            {
                return false;
            }

            return RowKey.Equals(u.RowKey);

        }

        /// <summary>
        /// Gets the hash code.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Gets or sets the user.
        /// </summary>
        /// <value>The user.</value>
        public String User
        {
            get
            {
                return PartitionKey;
            }
            set
            {
                PartitionKey = value;
            }
        }
        /// <summary>
        /// Gets or sets the recipents.
        /// </summary>
        /// <value>The recipents.</value>
        public List<String> Recipents { get; set; }
        /// <summary>
        /// Gets or sets the subject of the message.
        /// </summary>
        /// <value>The message's subject.</value>
        [DisplayName("Asunto")]
        [Required(ErrorMessage = "Debe introducir un asunto.")]
        public String Subject { get; set; }
        /// <summary>
        /// Gets or sets the body of the message.
        /// </summary>
        /// <value>The message's body.</value>
        [DisplayName("Texto")]
        public String Body { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Message"/> is read.
        /// </summary>
        /// <value><c>true</c> if the message was read; otherwise, <c>false</c>.</value>
        public bool Read { get; set; }
        /// <summary>
        /// Gets or sets the date of the message
        /// </summary>
        /// <value>The message's date.</value>
        public DateTime Date { get; set; }
    }
}
